package com.nti56.springboot.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nti56.springboot.exception.MyRuntimeException;
import com.nti56.springboot.mapper.tmsmapper.MachInfoMapper;
import com.nti56.springboot.model.MachInfo;
import com.nti56.springboot.utils.ParamUtil;
import com.nti56.springboot.utils.ParamUtil.Param;

@Service
public class MachInfoService {
	
	private static final Logger logger = LoggerFactory.getLogger(MachInfoService.class);

	
	@Autowired
	private MachInfoMapper machInfoMapper;
	
	public MachInfo getById(String pmId) {
		return this.machInfoMapper.getById(pmId);
	}
	
	public void insert(MachInfo machInfo) {
		Param param = ParamUtil.setParam("machNum", machInfo.getMachNum())
							   .setParam("ip", machInfo.getIp());
		Long cnt = machInfoMapper.countByMap(param);
		if(cnt > 0) {
			logger.error("机台编号:"+machInfo.getMachNum()+" 或   ip:"+machInfo.getIp()+" 已经存在请勿重复添加!");
			throw new MyRuntimeException("机台编号:"+machInfo.getMachNum()+" 或  ip:"+machInfo.getIp()+" 已经存在请勿重复添加!");
		}
		this.machInfoMapper.insert(machInfo);
	}
	
	public void update(MachInfo machInfo) {
		Param param = ParamUtil.setParam("machNum", machInfo.getMachNum())
				   				.setParam("ip", machInfo.getIp())
				   				.setParam("machId", machInfo.getMachId());					
		Long cnt = machInfoMapper.countByMap(param);
		if(cnt > 0) {
			logger.error("机台编号:"+machInfo.getMachNum()+" 或   ip:"+machInfo.getIp()+" 已经存在请勿重复添加!");
			throw new MyRuntimeException("机台编号:"+machInfo.getMachNum()+" 或  ip:"+machInfo.getIp()+" 已经存在请勿重复添加!");
		}
		this.machInfoMapper.update(machInfo);
	}
	
	public List<MachInfo> findAll(Map params) {
		return machInfoMapper.findAll(params);
	}
	
	@Transactional(rollbackFor = Exception.class, transactionManager = "tmsTransactionManager")
	public void delete(String machId){
		machInfoMapper.delete(machId);
	}
	
	
}
